package com.cmsky.open.user.service.impl;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.CookieUtil;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.user.manager.UserInfoManager;
import com.cmsky.open.user.model.UserInfo;
import com.cmsky.open.user.service.UserLoginService;
import com.cmsky.open.user.vo.query.UserInfoQuery;

@Service("userLoginService")
public class UserLoginServiceImpl implements UserLoginService{
	protected static final Logger logger = Logger.getLogger(UserLoginService.class);
	protected static final String userCookieName = "uuid";
	
	private ShardedJedisPool shardedJedisPool;
	private UserInfoManager userInfoManager;
	
	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}

	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	public UserInfo getLoginedUser(HttpServletRequest request,
			HttpServletResponse response) {
		UserInfo user = null;
		String uuid = null;
		Cookie cookie = CookieUtil.getCookie(request, response, userCookieName);
		if(null != cookie){
			uuid = cookie.getValue();
		}else{
			uuid = request.getParameter(userCookieName);
		}
		if(StringUtils.isNotBlank(uuid)){
			ShardedJedis jedis =  shardedJedisPool.getResource();
			String beanJson = jedis.get(uuid);
			if(null != beanJson){
				user = JsonUtils.objectFromJson(beanJson, UserInfo.class);
			}
		}
		return user;
	}

	public UserInfo lgIn(HttpServletRequest request,
			HttpServletResponse response, String email, String passwd) {
		UserInfoQuery query = new UserInfoQuery();
		UserInfo info = null;
		query.setEmail(email);
		query.setPasswd(passwd);
		BasePager<UserInfo> pager = this.userInfoManager.findPage(query);
		if(pager.getRows()!=null && pager.getRows().size()==1){
			info = (UserInfo) pager.getRows().get(0);
			logger.info("<|>lgIn<|>"+info+"<|>success<|>");
			String uuid = getUUID();
			String statusCode = shardedJedisPool.getResource().set(uuid, JsonUtils.jsonFromObject(info));
			if(null!=statusCode){
				CookieUtil.setCookie(request, response, userCookieName, uuid, 600);
			}
		}
		return info;
	}

	public void lgOut(HttpServletRequest request, HttpServletResponse response) {
		String uuid = null;
		Cookie cookie = CookieUtil.getCookie(request, response, userCookieName);
		if(null != cookie){
			uuid = cookie.getValue();
		}else{
			uuid = request.getParameter(userCookieName);
		}
		if(StringUtils.isNotBlank(uuid)){
			shardedJedisPool.getResource().del(uuid);
		}
	}

	public boolean updatePasswd(Integer shopId, String oldPasswd,
			String newPasswd) {
		boolean result = false;
		if(null!=shopId){
			UserInfoQuery query = new UserInfoQuery();
			query.setId(shopId);
			query.setPasswd(oldPasswd);
			BasePager pager = this.userInfoManager.findPage(query);
			if(null!=pager.getRows() && pager.getRows().size()==1){
				query.setPasswd(newPasswd);
				if(0 < this.userInfoManager.updatePass(query)){
					logger.info("<|>updatePasswd<|>"+shopId+"<|>success<|>");
					result = true;
				}
			}
		}
		return result;
	}
	
	protected String getUUID(){
		return "U"+UUID.randomUUID();
	}
}
